Import mojo
Import diddy
Import diddy.collections
Import model

Class LinkButton Extends Sprite
	Global MAX_BUTTONS% = 100
	Field buttonsList:ArrayList<LinkButton>=null
	Field event:ButtonEvent 
	Field highlight = false
	Field pressed = false
	Field hoverImage:Int
	Field mainImage:Int
	Field text:String
	Field width:Int
	Field currentTween:Int = TWEEN_IN
	Field nextNumber:Int
	Field tween3:Tween2
	Field buttonNdx:Int = 0
	Field originalX:Int
	Field originalY:Int
	
	Public
		Const TWEEN_IN:Int = 0
		Const TWEEN_OUT:Int = 1

			
	Function Create:LinkButton(gi:GameImage,text:String, x#, y#, event:ButtonEvent, frame:Int=0,buttonNdx:Int, buttonsList:ArrayList<LinkButton> )
	
		Local button:LinkButton = new LinkButton()
		button.buttonsList=buttonsList
		button.buttonsList.RangeChecking(false)
		button.image = gi
		button.x = x
		button.y = y
		button.originalX = x
		button.originalY = y
		button.alpha = 1
		button.SetHitBox(-gi.w2, -gi.h2, gi.w, gi.h)
		button.event = event
		button.mainImage = frame
		button.text = text
		button.width=500'_model.numbersFont.GetTxtWidth(text) ************************ CHANGE THIS TO BE MORE ACURATE **********************
		button.buttonNdx = buttonNdx
		button.tween3 = New Tween2(Tween2.Elastic.EaseOut,0,0,0)
		button.setState(TWEEN_IN)
		buttonsList.Add(button)
	
		Return button
	End
	
	Function StartAll:Void(buttonsList:ArrayList<LinkButton>)
		For Local ndx:Int=0 Until buttonsList.Size
			Local val:LinkButton = buttonsList.Get(ndx)
			val.setState(TWEEN_IN)
		End		
	End
	
	Function DrawAll:Void(buttonsList:ArrayList<LinkButton>)
		For Local ndx:Int=0 Until buttonsList.Size
			Local val:LinkButton = buttonsList.Get(ndx)
			val.Draw()
		End					
	End
	
	Function UpdateAll:Void(buttonsList:ArrayList<LinkButton>)
		For Local ndx:Int=0 Until buttonsList.Size
			Local val:LinkButton = buttonsList.Get(ndx)
			val.Update()
		End	
	End	
	
	Method setState:Void(state:Int)
		Select state
			Case TWEEN_IN
				' Tween the alpha
				tween3.SetEquation(Tween2.Elastic.EaseOut)
				tween3.SetDuration(750+(buttonNdx*100))
				tween3.SetValue(SCREEN_WIDTH+100, originalX)			
				tween3.Start()
'			Case TWEEN_OUT
'				tween3.SetEquation(Tween2.Elastic.EaseOut)
'				tween3.SetDuration(500+(buttonNdx*100))
'				tween3.SetValue(originalX, SCREEN_WIDTH+100)			
'				tween3.Start()						
		End
	End
	
	Method Draw:Void()
		Local offset:Int = 0
		
		alpha = 0.9
		
		if(highlight)
			alpha = 1.0
		ElseIf (pressed)
			offset = 1
		Else
			alpha = 0.9		
		Endif
		
	'	PushMatrix
	'	Translate (x,y)
		
		frame=mainImage
		Super.Draw(offset, offset)	
		
		if(highlight)
			frame=hoverImage
			Super.Draw(offset, offset)			
		Endif

		DrawText(text, x+offset+image.w+10,y+offset, 0,0.5)
	'	PopMatrix 
		
	End

	
	Method Update:Void()
	
		highlight=false
		pressed=false
		
		tween3.Update()
		x=tween3.Value()

		if RectsOverlap(x + hitBoxX, y + hitBoxY, width, hitBoxHeight, 
						game.mouseX, game.mouseY, 1, 1)
			If game.MouseHit()							
				event.OnClicked()
			ElseIf MouseDown()
				pressed=true				
			Else
				highlight=true
			EndIf
		End
	
	End
	
End

Class LinkEvent Abstract
	Method OnClicked:Void() Abstract
End





